import {observable, action,toJS} from 'mobx';
import { SearchAdvanced } from 'weaver-mobile-page';
import * as Api from '../../api/MyBlogApi';
import * as Count from '../../api/UserCenterApi'
import { PARAMS ,renderNoData} from '../../components/public/BlogUtils';
import BlogCommentStore from '../comment/BlogCommentStore';

const initalState = {
  currentPage: 1,
  blogPage: 1,
  photoSwipePage: 1,
  blogEndDate: '',
  commentCount: 0,
  BlogCommentStoreMap: {},
  isSearch: false,
  searchAdValue: '',
}

export default class WeiBoStore {

  @observable currentPage = initalState.currentPage;
  @observable photoSwipePage = initalState.photoSwipePage;//翻页图片预览
  @observable blogEndDate = initalState.blogEndDate;
  @observable commentCount = initalState.commentCount;
  @observable myBlog = new BlogCommentStore({ getComment: this.getMyBlogList });
  @observable comment = new BlogCommentStore({ getComment: this.getCommentList });
  @observable laud = new BlogCommentStore({ getComment: this.getLaudList });
  @observable atMe = new BlogCommentStore({ getComment: this.getAtMeList });
  @observable BlogCommentStoreMap = initalState.BlogCommentStoreMap;

  @observable isSearch = initalState.isSearch;
  @observable searchAdValue = initalState.searchAdValue; // 搜索值
  @observable search = new SearchAdvanced.Store({ route: '/userCenter/shareToMe/shareToMeWeibo', id:'myBlogAdsearch' });  // 高级搜索
  @observable blogPage = initalState.blogPage;
  @observable thisDate = "";
  @observable noSub = 0;
  @observable replyList = [];
  @action setStoreState = (params) => {
    Object.keys(params).map(key => {
      this[key] = params[key];
    })
  }

  // 请求评论，赞 数字
  getMenuCountData = () => {
    Count.getMenuCountData().then(data=> {
      this.commentCount = data.countData.commentCount;
    })
  }

  /* 我的微博--微博 */
  // 获得高级搜索数据
  blogSearchCondition = () => {
    this.search.getData({
      url:`/api/blog/base/blogSearchCondition`,
      method: `GET`,
      params: {...PARAMS, listType:'isShareToMeAll'}
    })
  }

  @action('请求我的微博-微博数据')
  getMyBlogList = () => {
    // if (this.isSearch||this.search.form.formParams.content !== '') {

      let searchParams = this.search.form.formParams || {};
      let params = {
        pageSize: 10,
        currentPage: this.blogPage++,
        ...searchParams,
      }
      return Api.getShareToMeBlogList(params).then(data => {
        const { status, replyList, currentPage, total,pageSize,thisDate,noSub } = data;
        let data_params = {hasMore: false}
        Object.assign(data, data_params)
        this.replyList = replyList;
        this.photoSwipePage++;
        this.thisDate = thisDate;
        this.noSub = noSub;
        return data
      })
    // } else {
    //   let params = {
    //     endDate: this.blogEndDate,
    //     pageSize: 10,
    //   }
    //   return Api.getShareToMeBlogList(params).then(data => {
    //     if (this.blogEndDate === '' && data.replyList[0].content === '' && data.replyList[0].replyId.indexOf('==') > -1) {

    //     } 
    //     if(data.status === '1'&& data.replyList.length>0) {
    //       if(!data.replyList.workdate){
    //           this.blogEndDate = data.replyList[data.replyList.length-1].workdate.split('-');
    //       }
    //       let temp = this.blogEndDate[this.blogEndDate.length-1]-1;
    //       this.blogEndDate[this.blogEndDate.length-1] = temp;
    //       this.blogEndDate = this.blogEndDate.join('-');

    //     }else {
    //         let data_params = {hasMore: false}
    //         Object.assign(data, data_params)
    //     }
    //      this.thisDate = data.thisDate;
    //      this.noSub = data.noSub;
    //       this.photoSwipePage++;
    //     return data;
    //   })
    //}
  }

  @action('关联高级搜索数据')
  updateAdSearchSubject = (value) => {
    this.searchAdValue = value;
    this.search.form.updateField('content', value);
  }

  @action('搜索')
  searchBlog = () => {
    this.isSearch = true;
    this.blogPage = 1;
    this.myBlog.blogCommentStore.getDatas();
  }

  /* 我的微博--评论 */
  @action('请求我的微博-评论数据')
  getCommentList = () => {
    let params = {
      type: 'reply',
      currentPage: this.currentPage++,
      pageSize: 10,
    }
    return Api.getMyblogListByType(params).then(data => {
      const { status, total, currentPage, pageSize } = data;

      if(status === '1' && total < currentPage * pageSize) {
        let data_params = {hasMore: false}
        Object.assign(data, data_params)
      }
      this.photoSwipePage++;
      return data
    })
  }

  /* 我的微博--赞 */
  @action('请求我的微博-赞数据')
  getLaudList = () => {
    let params = {
      type: 'zan',
      currentPage: this.currentPage++,
      pageSize: 10,
    }
    return Api.getMyblogListByType(params).then(data => {
      const { status, total, currentPage, pageSize } = data;
      if(status === '1' && total < currentPage * pageSize) {
        let data_params = {hasMore: false}
        Object.assign(data, data_params)
      }
      this.photoSwipePage++;
      return data
    })
  }

  /* 我的微博--@我 */
  @action('请求我的微博-@我数据')
  getAtMeList = () => {
    let params = {
      type: 'at',
      currentPage: this.currentPage++,
      pageSize: 10,
    }
    return Api.getMyblogListByType(params).then(data => {
      const { status, total, currentPage, pageSize } = data;
      if(status === '1' && total < currentPage * pageSize) {
        let data_params = {hasMore: false}
        Object.assign(data, data_params)
      }
      this.photoSwipePage++;
      return data
    })
  }
  
  setCommentCount = () => {
    this.commentCount = 0;
  }

  // 初始化数据
  initCondition = () => {
    this.setStoreState(initalState);
  }
}